const $api = require('../../utils/api').API.ad
const $cache = require('../../utils/storage')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '',
    pics: [],
    tags: [{
        id: 1,
        title: '校园劳务',
        isChoose: true
      },
      {
        id: 2,
        title: '驾校',
        isChoose: true
      },
      {
        id: 3,
        title: '考证',
        isChoose: true
      },
      {
        id: 4,
        title: '专升本',
        isChoose: true
      },
      {
        id: 5,
        title: '综合',
        isChoose: false
      },
    ],
    authorId: "",
  },
  handleDel(e){
    let index = e.target.dataset.index
    let pics = this.data.pics
    pics.splice(index,1)
    this.setData({
      pics
    })
    console.log(1111111111,e)
  },
  getText(e) {
    let text = e.detail.value
    console.log(text)
    this.setData({
      text
    })
  },
  handleSend() {
    let params = {
      content: this.data.text,
      authorId: this.data.authorId,
    }
    $api.addAd(params).then(res => {
      let id = res.data.data
      let pics = this.data.pics
      if(res.data.code==200){
        for (let i = 0; i < pics.length; i++) {
          wx.uploadFile({
            url: 'https://qdwlkj.cn/advisement/upImg ',
            filePath: pics[i],
            name: "img",
            formData: {
              id, //其他需要携带的参数
            },
            header: {
              "content-type": "multipart/form-data"
            },
            success(res) {
              wx.switchTab({
                url:'/pages/advertisement_new/advertisement_new'
              })
            },
            fail(err) {
              console.log(err,1);
            },
            complete(result) {
              console.log(result.errMsg,2)
            }
          })
        }
      }
    })
  },
  handleBack() {
    wx.switchTab({
      url:'/pages/home_new/home_new'
    })
  },
  handleAdd() {
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success: (res) => {
        console.log(res)
        let pics = res.tempFiles.map(item => item.tempFilePath)
        this.setData({
          pics: [...this.data.pics, ...pics]
        })
      }
    })
  },
  handleCheck(e) {
    let id = e.target.dataset.id
    let newTags = this.data.tags.map(item => {
      if (item.id == id) {
        item.isChoose = !item.isChoose
      }
      return item
    })
    this.setData({
      tags: [...newTags]
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let openId = $cache._getStorage('openId')
    this.setData({
      authorId:openId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})